package com.si.controller.manage;


import javax.servlet.http.HttpServletRequest;

import com.si.service.BlogrollService;
import com.si.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.si.entity.SysUser;
import com.si.service.SysUserService;

@Controller
@RequestMapping("sysUser/manage")
public class SysUserManagerController {
	@Autowired
	private OperationLogService operationLogService;
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 进入修改密码的页面
	 * @return
	 */
	@RequestMapping(value="initPassword")
	public String initPassword(
			ModelMap modelmap,
			@ModelAttribute("message") String message){
		modelmap.put("message", message);
		return "managePage/sysUser/passWord";
	}
	
	/**
	 * 修改新密码
	 * @param newPassword
	 * @return
	 */
	@RequestMapping(value="updatePassword")
	public String update(
			RedirectAttributes redirectAttributes,
			@RequestParam(value = "sysUserId", required = false) Integer sysUserId,
			@RequestParam(value = "password1", required = false) String password1,
			@RequestParam(value = "newPassword", required = false) String newPassword){
		if(sysUserId != null){
			SysUser user = (SysUser) sysUserService.getObjectById(sysUserId);
			user.setPassWord(newPassword);
			sysUserService.saveOrUpdate(user);
			redirectAttributes.addFlashAttribute("message","密码修改成功");
		}else{
			redirectAttributes.addFlashAttribute("message","当前登陆人为空");
		}
		return "redirect:/sysUser/manage/initPassword.html";
	}
	
	
	@RequestMapping(value="findSysUser")
	public String findSysUser(ModelMap modelmap,@ModelAttribute("message") String message){
		modelmap.put("message", message);
		return "managePage/sysUser/sysUser";
	}
	
	/**
	 * 修改新密码
	 * @param userName
	 * @param realname
	 * @param phoneNum
	 * @return
	 */
	@RequestMapping(value="save")
	public String save(
			RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@RequestParam(value = "sysUserId", required = false) Integer sysUserId,
			@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "realname", required = false) String realname,
			@RequestParam(value = "phoneNum", required = false) String phoneNum){
		SysUser sysUser =(SysUser)sysUserService.getObjectById(sysUserId);
		sysUser.setUserName(userName);
		sysUser.setPhoneNum(phoneNum);
		sysUser.setRealname(realname);
		sysUserService.saveOrUpdate(sysUser);
		redirectAttributes.addFlashAttribute("message","个人信息修改成功");
		request.getSession().setAttribute("session_sysUser", sysUser);
		return "redirect:/sysUser/manage/findSysUser.html";
	}
}
